#include "SetScriptDialog.h"

#include "qlist.h"
#include "qmessagebox.h"
#include "SetScriptWaitDialog.h"
#include "SetScriptLoopDialog.h"
#include  "SetThermostatDialog.h"
#include  "SetPositionModeDialog.h"
#include  "SetRateModeDialog.h"
#include "ScriptRunner.h"
#include "ScriptInstructionFactory.h"

SetScriptDialog::SetScriptDialog(QWidget* parent) :QDialog(parent)
{
	ui.setupUi(this);
	initSignalsAndSlots();
	ui.pushButton_script_edit->setDisabled(true);
}
SetScriptDialog::~SetScriptDialog() {}

int SetScriptDialog::exec()
{
	ui.treeWidget_script_list->showScriptList();
	return QDialog::exec();
}

void SetScriptDialog::accept()
{
	if (ui.treeWidget_script_list->updateScriptList())
	{
		QDialog::accept();
	}
	return;
}

void SetScriptDialog::slot_openSetPositionModeDialog()
{
	if (QDialog::Accepted == setPositionModeDialog->exec())
	{
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_PositionMode(setPositionModeDialog->getPosition(), setPositionModeDialog->getRate(), setPositionModeDialog->getAcceleration()));
	}
}
void SetScriptDialog::slot_openSetRateModeDialog()
{
	if (QDialog::Accepted == setRateModeDialog->exec())
	{
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_RateMode(setRateModeDialog->getRate(), setRateModeDialog->getAcceleration()));
	}
}
void SetScriptDialog::slot_openSetThermostatDialog()
{
	if (QDialog::Accepted == setThermostatDialog->exec())
	{
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_Thermostat_TemperatureRate(setThermostatDialog->getRate()));
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_Thermostat_TargetTemperature(setThermostatDialog->getTemperature()));
	}
}
void SetScriptDialog::slot_openSetWaitTimeDialog()
{
	if (QDialog::Accepted == setScriptWaitDialog->exec())
	{
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_Wait(setScriptWaitDialog->getWaitTime()));
	}
}
void SetScriptDialog::slot_openSetLoopDialog()
{
	if (QDialog::Accepted == setScriptLoopDialog->exec())
	{
		ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_Loop(setScriptLoopDialog->getLoopCount()));
	}
}
void SetScriptDialog::slot_edit() {}


void SetScriptDialog::initSignalsAndSlots()
{
	connect(ui.pushButton_turnTable_powerOn, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_PowerOn()); });
	connect(ui.pushButton_turnTable_powerOff, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_PowerOff()); });
	connect(ui.pushButton_turnTable_closeCircuit, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_CloseCircuit()); });
	connect(ui.pushButton_turnTable_openCircuit, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_OpenCircuit()); });
	connect(ui.pushButton_turnTable_unloadedMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_SetLoad(TurnTableCodec::Mode_Unloaded)); });
	connect(ui.pushButton_turnTable_loadedMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_SetLoad(TurnTableCodec::Mode_Loaded)); });
	connect(ui.pushButton_turnTable_setPositionMode, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetPositionModeDialog);
	connect(ui.pushButton_turnTable_setRateMode, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetRateModeDialog);
	connect(ui.pushButton_turnTable_runPositionMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_Run(TurnTableCodec::Mode_PositionMode)); });
	connect(ui.pushButton_turnTable_runRateMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_Run(TurnTableCodec::Mode_RateMode)); });
	connect(ui.pushButton_thermostat_setTemperatureAndRate, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetThermostatDialog);
	connect(ui.pushButton_turnTable_stop, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_Stop()); });
	connect(ui.pushButton_turnTable_emergencyStop, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_TurnTable_EmergencyStop()); });
	connect(ui.pushButton_dataReader_startCollection, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_DataReader_StartCollection()); });
	connect(ui.pushButton_dataReader_stopCollection, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(ScriptInstructionFactory::getDescribe_DataReader_StopCollection()); });
	connect(ui.pushButton_script_confirm, &QPushButton::clicked, this, &SetScriptDialog::accept);
	connect(ui.pushButton_script_cancel, &QPushButton::clicked, this, &SetScriptDialog::reject);
	connect(ui.pushButton_script_wait, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetWaitTimeDialog);
	connect(ui.pushButton_script_creatLoop, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetLoopDialog);
	connect(ui.pushButton_script_edit, &QPushButton::clicked, this, &SetScriptDialog::slot_edit);
	connect(ui.pushButton_script_clone, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::Clone); });
	connect(ui.pushButton_script_moveUp, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::MoveUp); });
	connect(ui.pushButton_script_moveDown, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::MoveDown); });
	connect(ui.pushButton_script_delete, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::Remove); });
}

SetScriptWaitDialog* SetScriptDialog::getSetScriptWaitDialog()
{
	return setScriptWaitDialog;
}
void SetScriptDialog::setSetScriptWaitDialog(SetScriptWaitDialog* setScriptWaitDialog)
{
	this->setScriptWaitDialog = setScriptWaitDialog;
}
SetScriptLoopDialog* SetScriptDialog::getSetScriptLoopDialog()
{
	return setScriptLoopDialog;
}
void SetScriptDialog::setSetScriptLoopDialog(SetScriptLoopDialog* setScriptLoopDialog)
{
	this->setScriptLoopDialog = setScriptLoopDialog;
}
SetThermostatDialog* SetScriptDialog::getSetThermostatDialog()
{
	return setThermostatDialog;
}
void SetScriptDialog::setSetThermostatDialog(SetThermostatDialog* setThermostatDialog)
{
	this->setThermostatDialog = setThermostatDialog;
}
SetPositionModeDialog* SetScriptDialog::getSetPositionModeDialog()
{
	return setPositionModeDialog;
}
void SetScriptDialog::setSetPositionModeDialog(SetPositionModeDialog* setPositionModeDialog)
{
	this->setPositionModeDialog = setPositionModeDialog;
}
SetRateModeDialog* SetScriptDialog::getSetRateModeDialog()
{
	return setRateModeDialog;
}
void SetScriptDialog::setSetRateModeDialog(SetRateModeDialog* setRateModeDialog)
{
	this->setRateModeDialog = setRateModeDialog;
}
ScriptRunner* SetScriptDialog::getScriptRunner()
{
	return scriptRunner;
}
void SetScriptDialog::setScriptRunner(ScriptRunner* scriptRunner)
{
	this->scriptRunner = scriptRunner;
	ui.treeWidget_script_list->setScriptRunner(this->scriptRunner);
}
